package client.comm.baoding.adapter.main

import android.content.Context
import android.content.Intent
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.databinding.ItemGrid1j4Binding
import client.comm.baoding.ui.GuoJiActivity
import client.comm.baoding.ui.TabOne2Activity
import client.comm.commlib.base.BaseListAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.commlib.dipToPx
import client.comm.commlib.getScreenWidth
import client.comm.commlib.toast.toast
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions

/**
 * 1+4
 *
 */
class HomeGood1j4Adapter : BaseListAdapter<GoodsIndex.CategoryIndex> {

    val mContext: Context
    val manager: RequestManager

    constructor(context: Context, manager: RequestManager) : super(context) {
        this.mContext = context
        this.manager = manager
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.item_grid_1j4,
                parent,
                false
            )
        ).apply {
            var itemBinding = binding as ItemGrid1j4Binding
            with(itemBinding) {
                val wi = (getScreenWidth(mContext) / 3) - dipToPx(mContext.resources, 20.0f)
//                root.layoutParams.width = wi.toInt()
//                root.layoutParams.height = wi.toInt()

                img.layoutParams.height = wi.toInt()
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        val binding = bindingViewHolder.binding as ItemGrid1j4Binding
        val bean = getItem(position)
        binding.bean = bean
        binding.event = this
        binding.position = position
//        if (bean.category_id.equals("12")) {
//            binding.root.visibility = View.VISIBLE
//        } else {
//            binding.root.visibility = View.GONE
//        }

        //img 1
        manager.load(bean.small_img)
            .apply(RequestOptions().error(R.mipmap.ic_launcher))
//            .skipMemoryCache(true) //禁止Glide内存缓存
//            .diskCacheStrategy(DiskCacheStrategy.NONE) //不缓存资源
            .thumbnail( 0.1f )
            .into(binding.img)

        if (position != 0) {
//            val wi = (getScreenWidth(context) / 3.6) - dipToPx(context.resources, 20.0f)

//            val w0 = (getScreenWidth(context) / 2) - dipToPx(context.resources, 10.0f)
//            val wi = (getScreenWidth(context) / 3) - dipToPx(context.resources, 20.0f)
//            binding.img.layoutParams.width = w0.toInt()
//            binding.img.layoutParams.height = wi.toInt()
        }

//        binding.tvTag.text = "${position}"
    }


    fun itemClick(bean: GoodsIndex.CategoryIndex, position: Int) {
//        var title = when (position) {
//            0-> "国际食品"
//            1-> "国际酒庄"
//            2-> "国际轻奢"
//            else ->"国际美妆"
//        }
        context.startActivity(Intent(context, GuoJiActivity::class.java).apply {
            putExtra("title_name", bean.name)
//            putExtra("position", 0)
        })
    }

}